package com.foreveross.fetl.core.domain.freight.repository.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.foreveross.fetl.common.jpa.EntityHibernateRepository;
import com.foreveross.fetl.core.domain.freight.LogMessage;
import com.foreveross.fetl.core.domain.freight.repository.ILogMessageRepository;

@Repository("logMessageRepository")
public class LogMessageRepositoryImpl extends EntityHibernateRepository
		implements ILogMessageRepository {

	@Override
	public List<LogMessage> findByType(int type) {
		return super.queryResult("select o from LogMessage o where executeType=?", new Object[]{type});
	}

	@Override
	public LogMessage getOneLogMessage(String executeClass, String executeName,
			Date executeStartTime, int executeType) {
		List<Object> conditionVals = new ArrayList<Object>();
		StringBuilder jpql =new StringBuilder("select logMes from LogMessage logMes where 1=1 ");
		if(executeClass!=null && !"".equals(executeClass)){
			jpql.append(" and logMes.executeClass = ? ");
			conditionVals.add(executeClass.split("\\.")[executeClass.split("\\.").length-1]);
		}
		if(null!=executeName && !"".equals(executeName)){
			jpql.append(" and logMes.executeName = ? ");
			conditionVals.add(executeName);
		}
		if(executeType!=0){
			jpql.append(" and logMes.executeType = ? ");
			conditionVals.add(executeType);
		}
		if(null!=executeStartTime){
			jpql.append(" and logMes.executeStartTime = ? ");
			conditionVals.add(executeStartTime);
		}
		jpql.append(" order by logMes.executeStartTime desc ");
		List<LogMessage> list =super.queryResult(jpql.toString(), conditionVals.toArray());
		if(null !=list && list.size()>0){
			return list.get(0);
		}
		return null;
	}

}
